<?php
################################################################################
//Start de sessie
session_start();

// Database connection
include 'includes/connect.php';

$total = '0';
################################################################################
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> Bestellen </title> 
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link type="text/css" href="css/pagination.css" rel="stylesheet" />
  <link rel="stylesheet" href="css/reset.css" type="text/css" />
  <link rel="stylesheet" href="css/960.css" type="text/css" />
  <link rel="stylesheet" href="css/text.css" type="text/css" />
  <link type="text/css" href="css/tablesorter.css" rel="stylesheet" />
  <link type="text/css" href="css/sunny/jquery-ui-1.8.custom.css" rel="stylesheet" />
  <link type="text/css" href="css/jquery.autocomplete.css" rel="stylesheet" />
  <link type="text/css" href="css/style.css" rel="stylesheet" />
  <link type="text/css" href="css/accmenu.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js">
</script>
<script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js">
</script>
<script type="text/javascript" src="js/jquery.tablesorter.js">
</script>
<script type="text/javascript" src="js/jquery.autocomplete.min.js">
</script>
<script type="text/javascript" src="js/menu.js">
</script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function()
    {
        $("#producttable").tablesorter();
        $("#searchtext").autocomplete("autocomplete.php");
        $("#searchtext").result(function(event, data, formatted) {
			$("#searchform").trigger('submit');
	    });

        $("#loginbox").dialog({
			autoOpen: false,
			height: 230,
			width: 350,
			modal: true,
            buttons: {
				'Login': logincall,
				Cancel: function() {
					$(this).dialog('close');
				}
                }
		});

        $("#login button").button({
            icons: {
                primary: 'ui-icon-locked'
            }
        })
        .click(function() {
				$('#loginbox').dialog('open');
			});
        $("#register button").button();
        $("#logout button").button();
        $("#shoppingcart button").button();

        $("#errorlogin").click(function() {
				$('#loginbox').dialog('open');
			});
    }
);

function logincall() {
    $.ajax({
    type: "POST",
    url: "login.php",
    data: $("#loginform").serialize(),
    dataType: "text",
    complete: function() {
      $(this).dialog('close');
      $(location).attr('href', "order.php");
    }
    });
  return;
}

//]]>
</script>
</head>

<body>
  <div class="container_12">
    <div id="header" class="grid_12">
      <div class="grid_5 prefix_7 alpha omega">
        <form id="searchform" name="searchform" method="get" action="search.php">
          <input type="text" name="q" id="searchtext" /> <button type="submit" id=
          "searchbutton"></button>
        </form>

        <div id="demo">
          <ul>
            <?php if(!empty($_SESSION['klantid'])){?>
            <li id="logout"><a href="logout.php"><button>Log Out</button></a></li>
            <li id="shoppingcart"><a href="view_cart.php"><button>Winkelwagen</button></a></li>
            <?php } else {?>
            <li id="login"><button>Log In</button></li>
            <li id="register"><a href="register.php"><button>Registreer</button></a></li>
            <?php } ?>
          </ul>


        </div>
        <?php if(!empty($_SESSION['klantid'])){?>
                        <p id="klantwelkom">Welkom <b><a href="account.php"><?php echo $_SESSION['fullname']; ?></a></b>&nbsp;&nbsp;
                        </p><?php } else { ?>
                        <p id="klantwelkom"><a href="forgot_pass.php">Wachtwoord vergeten?</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
                        <?php } ?>
      </div>

      <div class="clear"></div>
    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>

    <div id="menu" class="grid_2">

          <?php
          include_once 'temp/menu_filler.php';
          ?>


    </div>

    <div id="mainarea" class="grid_7">
        <div id="tableholder">
        <?php

        if(empty($_SESSION['klantid']))
        {
                $error='U bent niet herkend als klant.<br /> <a id="errorlogin" href="#" title="login">Log in</a> of <a href="register.php" title="register">registreer</a> aub.';
        }
        else
        {
        	if(!empty($_SESSION['winkelwagen']))
        	{
              // Max productid zoeken
              $result = mysql_query("SELECT max(factuurid) FROM factuur")
              or die(mysql_error());
              $row = mysql_fetch_array( $result );
              $fid=$row[0]+1;

              $command = "INSERT INTO factuur (klantid,factuurid) VALUES('".$_SESSION['klantid']."','$fid')";
              $query  = mysql_query($command) or die("SQL error: ".mysql_error());

        		foreach($_SESSION['winkelwagen'] as $key=>$value)
        		{
                    $command4 = "SELECT * FROM product WHERE productid=".$key."";
              		$query4  = mysql_query($command4) or die("SQL error: ".mysql_error());
              		$row4 = mysql_fetch_array($query4);

                    $now=mktime();
                    if ($row4['begindate'] < $now && $row4['enddate'] > $now )
                    {
                        $price=$row4['Promotieprijs'];
                    }
                    else
                    {
                        $price=$row4['stdprijs'];
                    }

                    $command2 = "INSERT INTO factuurregel (klantid,productid,factuurid,aantal,vkprijs) VALUES('".$_SESSION['klantid']."','$key','$fid','$value','$price')";
                    $query2  = mysql_query($command2) or die("SQL error: ".mysql_error());
        		}


              	foreach($_SESSION['winkelwagen'] as $key=>$value)
              	{
              	    $command = "SELECT * FROM product WHERE productid=".$key."";
              		$query2  = mysql_query($command) or die("SQL error: ".mysql_error());
              		$row2 = mysql_fetch_array($query2);

                    $now=mktime();
                    if ($row2['begindate'] < $now && $row2['enddate'] > $now )
                    {
                        $price=$row2['Promotieprijs'];
                    }
                    else
                    {
                        $price=$row2['stdprijs'];
                    }

                    // opnieuw totaalbedrag calculeren ivm veiligheid
              		$total += $value*$price;

                    ############################################### VOORRAAD HERBEREKENEN ################################
                    $oldstock = $row2['voorraad'];

                    if(($oldstock-$value) < 0)
                    {
                        $newstock = 0;
                    }
                    else
                    {
                        $newstock = ($oldstock-$value);
                    }

                    $vrq = "UPDATE product SET voorraad='$newstock' WHERE productid='$key'";
                    $vrcalc = mysql_query($vrq) or die("SQL error: ".mysql_error());
                    #######################################################################################################

              	}

                echo "Totaalbedrag van de order: <b>&euro;";
                echo number_format($total,2);
                echo "</b><form action=\"ideal/index.php\" method=\"post\"> <input type=\"hidden\" name=\"total\" value=\"$total\" /><input type=\"submit\" name=\"submit\" value=\"doorgaan\"></form></p>";

        	}
        	else
            {
        		$error="Er staat nog niks in uw winkelwagen.";
        	}
        }


        		if(!empty($msg))echo $msg;
                if(!empty($error))echo $error;
        ?>

        </div>
    </div>

    <div id="sidebar" class="grid_3"><div id="tableholder"><?php include 'cart.php'; ?></div></div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>
</div>
<div id="loginbox">
                <form action="#" method="POST" id="loginform">
                <br /><p><label for="email">Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input type="text" value="" name="email" class="text ui-widget-content ui-corner-all" /></p>
                <p><label for="wachtwoord">Wachtwoord:</label>
                <input type="password" value="" name="wachtwoord" class="text ui-widget-content ui-corner-all" /></p>
</form>
</div>
<center>
<!-- Begin Motigo Webstats counter code -->
<a id="mws4736564" href="http://webstats.motigo.com/">
<img width="80" height="15" border="0" alt="Free counter and web stats" src="http://m1.webstats.motigo.com/n80x15.gif?id=AEhGNAHBHVZi00ZSyCakWn8n38NA" /></a>
<script src="http://m1.webstats.motigo.com/c.js?id=4736564&amp;lang=NL&amp;i=3" type="text/javascript"></script>
<!-- End Motigo Webstats counter code -->
</center>
<br />

</body>
</html>
